<?php
/**
 * 青少年模式
 */

namespace App\Http\Controllers\App\V1;

use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;        //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use App\Response\Response;

class TeenagerController extends BaseController
{

    public function info()
    {
        $info = app('repo_teenager')->getTeenagerInfo(UID);
        $today_pop = app('redis')->get('teenager:' . UID . ':' . date('Ymd'));
        $today_pop = $today_pop == 1 ? 1 : 0;
        return Response::sendData(
            [
                'status'       => $info->status,//1开启2关闭
                'today_pop'    => $today_pop,
                'had_password' => $info->password ? 1 : 0,
            ]
        );
    }

    public function open(Request $request)
    {
        $password = $request->input('password');
        if (!$password) {
            throw new BasicException(0, '请输入密码');
        }
        $info = app('repo_teenager')->getTeenagerInfo(UID);
        if (!$info->password) {
            throw new BasicException(0, '请先设置密码');
        }
        if ($info->status == 1) {
            throw new BasicException(0, '您已开启了青少年模式');
        }
        if (!Hash::check($password, $info->password)) {
            throw new BasicException(0, '密码错误');
        }
        app('model_teenager')->where('user_id', UID)->update(
            [
                'status' => 1
            ]
        );
        app('model_teenager_log')->create(
            [
                'user_id'  => UID,
                'status'   => 1,
                'add_time' => Carbon::now()->timestamp
            ]
        );
        return Response::sendData('开启成功');
    }

    public function close(Request $request)
    {
        $password = $request->input('password');
        if (!$password) {
            throw new BasicException(0, '请输入密码');
        }
        $info = app('repo_teenager')->getTeenagerInfo(UID);
        if (!$info->password) {
            throw new BasicException(0, '请先设置密码');
        }
        if ($info->status == 2) {
            throw new BasicException(0, '您已关闭了青少年模式');
        }
        if (!Hash::check($password, $info->password)) {
            throw new BasicException(0, '密码错误');
        }
        app('model_teenager')->where('user_id', UID)->update(
            [
                'status' => 2
            ]
        );
        app('model_teenager_log')->create(
            [
                'user_id'  => UID,
                'status'   => 2,
                'add_time' => Carbon::now()->timestamp
            ]
        );
        return Response::sendData('关闭成功');
    }

    public function password(Request $request)
    {
        $password = $request->input('password');
        $new_password = $request->input('new_password');
        if (strlen($password) != 4) {
            throw new BasicException(0, '请输入4位密码');
        }

        $info = app('repo_teenager')->getTeenagerInfo(UID);
        if ($info->password) {
            if (strlen($new_password) != 4) {
                throw new BasicException(0, '请输入4位密码');
            }
            if (!Hash::check($password, $info->password)) {
                throw new BasicException(0, '密码错误');
            }
            $password = $new_password;
        }

        app('model_teenager')->where('user_id', UID)->update(
            [
                'password' => Hash::make($password)
            ]
        );
        app('model_teenager_log')->create(
            [
                'user_id'  => UID,
                'type'     => 2,
                'add_time' => Carbon::now()->timestamp
            ]
        );
        return Response::sendData('修改成功');
    }

    public function closePop()
    {
        $redis_key = 'teenager:' . UID . ':' . date('Ymd');
        if (!app('redis')->setnx($redis_key, 1)) {
            throw new BasicException(0, '已操作过');
        }
        app('redis')->expireat($redis_key, Carbon::tomorrow()->timestamp);

        app('model_teenager_log')->create(
            [
                'user_id'  => UID,
                'add_time' => Carbon::now()->timestamp,
                'type'     => 3
            ]
        );
        return Response::sendData('关闭成功');
    }

    public function verifyPassword(Request $request)
    {
        $password = $request->input('password');
        if (strlen($password) != 4) {
            throw new BasicException(0, '请输入4位密码');
        }

        $info = app('repo_teenager')->getTeenagerInfo(UID);
        if (!Hash::check($password, $info->password)) {
            throw new BasicException(0, '密码错误');
        }
        return Response::sendData('密码正确');
    }

    public function resetPassword(Request $request)
    {
        $user_id = $request->input('user_id');
        if (!$user_id) {
            throw new BasicException(0, '请选择用户');
        }
        app('model_teenager')->where('user_id', $user_id)->update(
            [
                'password' => '',
                'status' => 0,
            ]
        );
        $redis_key = 'teenager:' . $user_id . ':' . date('Ymd');
        app('redis')->del($redis_key);
        app('model_teenager_log')->create(
            [
                'user_id'  => $user_id,
                'type'     => 3,
                'add_time' => Carbon::now()->timestamp
            ]
        );
        return Response::sendData('重置成功');
    }
}
